package cn.edu.zafu.bigdata.lekang.shiyan8;

import java.io.*;
import java.net.*;

public class Client {
    public static void main(String[] args) {
        try {
            // 创建客户端套接字，连接服务器
            Socket socket = new Socket("192.168.16.69", 1234);
            System.out.println("Connected to server.");

            // 使用socket的输出流发送消息给服务器
            PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
            // 使用socket的输入流读取服务器的回复
            BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));

            // 读取用户输入并发送给服务器
            BufferedReader stdInput = new BufferedReader(new InputStreamReader(System.in));
            String userInput;
            System.out.println("Enter messages ('bye' to quit):");
            while ((userInput = stdInput.readLine()) != null) {
                out.println(userInput);

                // 读取服务器的回复
                String serverReply = in.readLine();
                System.out.println("Server says: " + serverReply);

                // 如果用户输入"bye"，则结束会话
                if ("bye".equalsIgnoreCase(userInput)) {
                    break;
                }
            }

            // 关闭连接
            stdInput.close();
            in.close();
            out.close();
            socket.close();
        } catch (UnknownHostException e) {
            System.err.println("Don't know about host.");
            e.printStackTrace();
        } catch (IOException e) {
            System.err.println("Couldn't get I/O for the connection to host.");
            e.printStackTrace();
        }
    }
}